<?php


namespace App\Http\Controllers\StoreCenter;


use App\Exceptions\ValidatorException;
use App\Http\Controllers\Controller;
use App\Http\Repositories\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CartController extends Controller
{
    private $repository;
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }

    public function getCartInfo(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required|int',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getCartInfo($validator->getData());
    }

    public function getCartList(Request $request)
    {
        //验证规则
        $rules = [
            'create_user' => 'nullable|string',
            'create_time' => 'nullable|array',
            'sku' => 'nullable|string',
            'spu' => 'nullable|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getCartList($validator->getData());
    }

    public function addCart(Request $request)
    {
        //验证规则
        $rules = [
            'spu' => 'required|string',
            'sku' => 'required|string',
            'attribute' => 'required|string',
            'sku_info' => 'required',
            'amount' => 'required|numeric',
            'price' => 'required|numeric',
            'actual_fee' => 'required|numeric',
            'discount' => 'required|numeric',
            'summary' => 'required|numeric',
            'shipping_fee' => 'required|numeric',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addCart($validator->getData());
    }

    public function delCart(Request $request)
    {
        //验证规则
        $rules = [
            'ids' => 'required'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->delCart($validator->getData());
    }

}
